package com.oisix.oisystemts2.service.autotask;

import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.Flushable;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.TimeZone;

import javax.mail.internet.InternetAddress;

import org.hibernate.Hibernate;
import org.hibernate.SQLQuery;
import org.hibernate.criterion.CriteriaSpecification;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;

import com.oisix.common.TestData;
import com.oisix.common.TestEnvironment;
import com.oisix.oisystemec.dao.KokyakuDao;
import com.oisix.oisystemec.exception.SystemException;
import com.oisix.oisystemec.model.AutoTaskHistory;
import com.oisix.oisystemec.service.autotask.AutoTaskResult;
import com.oisix.oisystemec.service.autotask.ExecuteDecision;
import com.oisix.oisystemfr.persistence.EntityManager;
import com.oisix.oisystemfr.persistence.EntityManagerFactory;
import com.oisix.oisystemfr.util.CsvUtil;
import com.oisix.oisystemfr.util.CsvWriter;
import com.oisix.oisystemts2.action.externalorder.LineMallCsv;
import com.oisix.test.common.TestException;

public class PointBalanceServiceTest {

    private static TestData testData;

    @BeforeClass
    public static void prepare() {
        TestEnvironment.prepare();
        testData = new TestData(
                "src/com/oisix/oisystemts2/service/autotask/PointBalanceServiceTest.xls");
        testData.load();
        TimeZone.setDefault(TimeZone.getTimeZone("JST"));

    }

    @After
    public void after() throws Exception {
        TestEnvironment.changeOisixSite();
        TestEnvironment.setDefaultTestDate();
    }

    /**
     * Test01 Method Test: siteCode().
     * 
     * Sub method: autotaskResult().
     * 
     * Test content: output pointo.csv file after finishing batch.
     * 
     * Test condition: Gochiflg=0 (oisix)|| Gochiflg=1 (gochi)
     * 
     * Expected Result: create csv file successfully csv file name:pointo.csv
     * content of csv file:91
     */

    @SuppressWarnings("unchecked")
    @Test
    public void testKokyakuDaoGetPointBalance01() {

        System.setProperty("site.code", "1");
        PointBalanceService pointBalanceService = new PointBalanceService();

        AutoTaskHistory history = new AutoTaskHistory();

        String userId = "9000";
        pointBalanceService.execute(history, userId);

        pointBalanceService.getAutoTaskName();
        pointBalanceService.getAutoTaskType();
        pointBalanceService.getGochiFlg();

        // GetsiteCode.
        EntityManager em = EntityManagerFactory.getEntityManager();
        List<Map<String, Object>> resultMap = new ArrayList<Map<String, Object>>();
        StringBuilder sb = new StringBuilder();
        sb.append("select gochi_flg from m_Kokyaku");

        SQLQuery query = em.createSQLQuery(sb.toString());
        query.addScalar("gochi_flg", Hibernate.STRING);

        resultMap = query.setResultTransformer(
                CriteriaSpecification.ALIAS_TO_ENTITY_MAP).list();

        // Confirm Sitecode
        assertThat((String) resultMap.get(0).get("gochi_flg"), is("0"));
        assertThat((String) resultMap.get(4).get("gochi_flg"), is("1"));

        // Test set Object
        pointBalanceService.setAutoTaskName("A0021");
        pointBalanceService.setAutoTaskType("01");
        pointBalanceService.setExecuteDecision(null);
        pointBalanceService.setGochiFlg(-1);

        // Get pointBalanceList
        List<Map<String, Object>> pointBalanceList = KokyakuDao
                .getPointBalance();

        // Get content of csv file

        try {

            List<String[]> csvContent = CsvUtil.getLines(new File(
                    "file/temp_file/pointo.csv"), "Windows-31J");
            assertThat(pointBalanceList.size(), is(7));
            assertThat(csvContent.get(0)[0], is("12345679"));
            assertThat(csvContent.get(0)[1], is("追六 太郎2"));
            assertThat(csvContent.get(0)[2], is("オイロク タロウニ"));
            assertThat(csvContent.get(0)[3], is("11"));
            assertThat(csvContent.get(0)[4], is("2014/08/01 07:59:59"));
            assertThat(csvContent.get(0)[5], is("0"));

        } catch (FileNotFoundException e) {
            e.printStackTrace();

        } catch (IOException e) {
            e.printStackTrace();

        }

        assertThat(pointBalanceService.getAutoTaskName(), is("A0021"));
        assertThat(pointBalanceService.getAutoTaskType(), is("01"));
        assertThat(pointBalanceService.getGochiFlg(), is(-1));
        assertThat(pointBalanceService.getGochiFlgCaption(), is("共通"));

        // mailFrom
        PointBalanceService service = new PointBalanceService();
        InternetAddress resultMailAddressFrom = new InternetAddress();
        service.setResultMailAddressFrom(resultMailAddressFrom);
        assertThat(service.getResultMailAddressFrom(),
                is(resultMailAddressFrom));

        // mailTo
        List<InternetAddress> resultMailAddressTo = new ArrayList<InternetAddress>();
        service.setResultMailAddressTo(resultMailAddressTo);
        assertThat(service.getResultMailAddressTo(), is(resultMailAddressTo));

    }

    /**
     * Test001 Method Test: siteCode().
     * 
     * Sub method: autotaskResult().
     * 
     * Test content: output pointo_summary.csv file after finishing batch
     * 
     * Test condition: Gochiflg=0 (oisix)|| Gochiflg=1 (gochi)
     * 
     * Expected Result: create csv file successfully csv file
     * name:pointo_summary.csv content of csv file:91
     */

    @SuppressWarnings("unchecked")
    @Test
    public void testKokyakuDaoGetPointBalance001() {

        System.setProperty("site.code", "1");
        PointBalanceService pointBalanceService = new PointBalanceService();

        AutoTaskHistory history = new AutoTaskHistory();
        String userId = "9000";
        pointBalanceService.execute(history, userId);

        // GetsiteCode.
        EntityManager em = EntityManagerFactory.getEntityManager();
        List<Map<String, Object>> resultMap = new ArrayList<Map<String, Object>>();
        StringBuilder sb = new StringBuilder();
        sb.append("select gochi_flg from m_Kokyaku");

        SQLQuery query = em.createSQLQuery(sb.toString());
        query.addScalar("gochi_flg", Hibernate.STRING);

        resultMap = query.setResultTransformer(
                CriteriaSpecification.ALIAS_TO_ENTITY_MAP).list();

        // Confirm Sitecode
        assertThat((String) resultMap.get(0).get("gochi_flg"), is("0"));
        assertThat((String) resultMap.get(4).get("gochi_flg"), is("1"));

        // Get pointBalanceList
        List<Map<String, Object>> pointBalanceList = KokyakuDao
                .getPointSummaryBalance();

        // Get content of csv file
        try {

            List<String[]> csvContent = CsvUtil.getLines(new File(
                    "file/temp_file/pointo_summary.csv"), "UTF-8");
            assertThat(pointBalanceList.size(), is(1));
            assertThat(csvContent.get(0)[0], is("91"));

        } catch (FileNotFoundException e) {
            e.printStackTrace();
            fail();
        } catch (IOException e) {
            e.printStackTrace();
            fail();
        }

        // mailFrom
        PointBalanceService interPointFrm = new PointBalanceService();
        InternetAddress resultMailAddressFrom = new InternetAddress();
        resultMailAddressFrom.setAddress("example@email.com");
        interPointFrm.setResultMailAddressFrom(resultMailAddressFrom);

        assertThat(resultMailAddressFrom.getAddress(), is("example@email.com"));

        assertThat(interPointFrm.getResultMailAddressFrom(),
                is(resultMailAddressFrom));

        // mailTo

        List<InternetAddress> resultMailAddressTo = new ArrayList<InternetAddress>();
        InternetAddress email = new InternetAddress();
        email.setAddress("exampleTo@email.com");
        resultMailAddressTo.add(email);

        assertThat(email.getAddress(), is("exampleTo@email.com"));

    }

    /**
     * Test002 Method Test: excute().
     * 
     * Sub method: createPointBalanceFile().
     * 
     * Test content: output csv file after finishing batch., check path File
     * 
     * Test condition:N/A
     * 
     * Expected Result: create csv file successfully csv file name:pointo.csv
     */
    @Test
    public void testKokyakuDaoGetPointBalance002() {

        PointBalanceService pointBalanceService = new PointBalanceService();
        pointBalanceService.getDefinition();

        AutoTaskHistory history = new AutoTaskHistory();
        String userId = "9000";
        pointBalanceService.execute(history, userId);

        // Get content of csv file
        try {

            List<String[]> csvContent = CsvUtil.getLines(new File(
                    "file/temp_file/pointo.csv"), "Windows-31J");

            assertThat(csvContent.get(1)[0], is("12345678"));
            assertThat(csvContent.get(1)[1], is("追六 太郎1"));
            assertThat(csvContent.get(1)[2], is("オイロク タロウイチ"));
            assertThat(csvContent.get(1)[3], is("10"));
            assertThat(csvContent.get(1)[4], is("2014/08/01 07:59:59"));
            assertThat(csvContent.get(1)[5], is("1"));

        } catch (FileNotFoundException e) {
            e.printStackTrace();
            fail();
        } catch (IOException e) {
            e.printStackTrace();
            fail();
        }

    }

    /**
     * Test03 Method Test: excute().
     * 
     * Sub method: createPointBalanceFile().
     * 
     * Test content: output csv file after finishing batch., check path File
     * 
     * Test condition:N/A
     * 
     * Expected Result: create csv file successfully csv file name:pointo.csv
     */

    @Test
    public void testKokyakuDaoGetPointBalance03() {

        PointBalanceService pointBalanceService = new PointBalanceService();

        AutoTaskHistory history = new AutoTaskHistory();
        String userId = "9000";
        pointBalanceService.execute(history, userId);

        // Get content of csv file
        try {

            List<String[]> csvContent = CsvUtil.getLines(new File(
                    "file/temp_file/pointo.csv"), "Windows-31J");

            assertThat(csvContent.get(2)[0], is("12345680"));
            assertThat(csvContent.get(2)[1], is("追六 太郎3"));
            assertThat(csvContent.get(2)[2], is("オイロク タロウサン"));
            assertThat(csvContent.get(2)[3], is("12"));
            assertThat(csvContent.get(2)[4], is("2014/08/01 07:59:59"));
            assertThat(csvContent.get(2)[5], is("0"));

        } catch (FileNotFoundException e) {
            e.printStackTrace();
            fail();
        } catch (IOException e) {
            e.printStackTrace();
            fail();
        }

        // mailFrom
        PointBalanceService interPointFrm = new PointBalanceService();
        InternetAddress resultMailAddressFrom = new InternetAddress();
        resultMailAddressFrom.setAddress("example@email.com");
        interPointFrm.setResultMailAddressFrom(resultMailAddressFrom);

        assertThat(resultMailAddressFrom.getAddress(), is("example@email.com"));

        assertThat(interPointFrm.getResultMailAddressFrom(),
                is(resultMailAddressFrom));

        // mailTo

        List<InternetAddress> resultMailAddressTo = new ArrayList<InternetAddress>();
        InternetAddress email = new InternetAddress();
        email.setAddress("exampleTo@email.com");
        resultMailAddressTo.add(email);

        assertThat(email.getAddress(), is("exampleTo@email.com"));

    }

    /**
     * Test04 Method Test: execute().
     * 
     * Sub method: createPointBalanceFile().
     * 
     * Test content: output csv file after finishing batch, check path File
     * 
     * Test condition:N/A
     * 
     * Expected Result: create csv file successfully csv file name:pointo.csv
     */

    @SuppressWarnings("unchecked")
    @Test
    public void testKokyakuDaoGetPointBalance04() {

        PointBalanceService pointBalanceService = new PointBalanceService();

        AutoTaskHistory history = new AutoTaskHistory();
        String userId = "9000";
        pointBalanceService.execute(history, userId);

        pointBalanceService.getAutoTaskName();
        pointBalanceService.getAutoTaskType();
        pointBalanceService.getGochiFlg();

        // Get content of csv file
        try {

            List<String[]> csvContent = CsvUtil.getLines(new File(
                    "file/temp_file/pointo.csv"), "Windows-31J");

            assertThat(csvContent.get(3)[0], is("12345681"));
            assertThat(csvContent.get(3)[1], is("追六 太郎4"));
            assertThat(csvContent.get(3)[2], is("オイロク タロウヨン"));
            assertThat(csvContent.get(3)[3], is("13"));
            assertThat(csvContent.get(3)[4], is("2014/08/01 07:59:59"));
            assertThat(csvContent.get(3)[5], is("1"));

        } catch (FileNotFoundException e) {
            e.printStackTrace();
            fail();
        } catch (IOException e) {
            e.printStackTrace();
            fail();
        }

        // mailFrom
        PointBalanceService interPointFrm = new PointBalanceService();
        InternetAddress resultMailAddressFrom = new InternetAddress();
        resultMailAddressFrom.setAddress("example@email.com");
        interPointFrm.setResultMailAddressFrom(resultMailAddressFrom);

        assertThat(resultMailAddressFrom.getAddress(), is("example@email.com"));

        assertThat(interPointFrm.getResultMailAddressFrom(),
                is(resultMailAddressFrom));

        // mailTo

        List<InternetAddress> resultMailAddressTo = new ArrayList<InternetAddress>();
        InternetAddress email = new InternetAddress();
        email.setAddress("exampleTo@email.com");
        resultMailAddressTo.add(email);

        assertThat(email.getAddress(), is("exampleTo@email.com"));

    }

    /**
     * Test05 Method Test: excute().
     * 
     * Sub method: createPointBalanceFile().
     * 
     * Test content: output csv file after finishing batch., check path File
     * 
     * Test condition:N/A
     * 
     * Expected Result: create csv file successfully csv file name:pointo.csv
     */

    @SuppressWarnings("unchecked")
    @Test
    public void testKokyakuDaoGetPointBalance05() {

        PointBalanceService pointBalanceService = new PointBalanceService();

        AutoTaskHistory history = new AutoTaskHistory();
        String userId = "9000";
        pointBalanceService.execute(history, userId);

        // Get content of csv file
        try {

            List<String[]> csvContent = CsvUtil.getLines(new File(
                    "file/temp_file/pointo.csv"), "Windows-31J");

            assertThat(csvContent.get(4)[0], is("12345682"));
            assertThat(csvContent.get(4)[1], is("追六 太郎1"));
            assertThat(csvContent.get(4)[2], is("オイロク タロウイチ"));
            assertThat(csvContent.get(4)[3], is("14"));
            assertThat(csvContent.get(4)[4], is("2012/02/01 00:00:00"));
            assertThat(csvContent.get(4)[5], is("1"));

        } catch (FileNotFoundException e) {
            e.printStackTrace();
            fail();
        } catch (IOException e) {
            e.printStackTrace();
            fail();
        }

        // mailFrom
        PointBalanceService interPointFrm = new PointBalanceService();
        InternetAddress resultMailAddressFrom = new InternetAddress();
        resultMailAddressFrom.setAddress("example@email.com");
        interPointFrm.setResultMailAddressFrom(resultMailAddressFrom);

        assertThat(resultMailAddressFrom.getAddress(), is("example@email.com"));

        assertThat(interPointFrm.getResultMailAddressFrom(),
                is(resultMailAddressFrom));

        // mailTo

        List<InternetAddress> resultMailAddressTo = new ArrayList<InternetAddress>();
        InternetAddress email = new InternetAddress();
        email.setAddress("exampleTo@email.com");
        resultMailAddressTo.add(email);

        assertThat(email.getAddress(), is("exampleTo@email.com"));

    }

    /**
     * Test06 Method Test: execute().
     * 
     * Sub method: createPointBalanceFile().
     * 
     * Test content: output csv file after finishing batch., check path File
     * 
     * Test condition:N/A
     * 
     * Expected Result: create csv file successfully csv file name:pointo.csv
     */

    @SuppressWarnings("unchecked")
    @Test
    public void testKokyakuDaoGetPointBalance06() {

        PointBalanceService pointBalanceService = new PointBalanceService();

        AutoTaskHistory history = new AutoTaskHistory();
        String userId = "9000";
        pointBalanceService.execute(history, userId);

        // Get content of csv file
        try {

            List<String[]> csvContent = CsvUtil.getLines(new File(
                    "file/temp_file/pointo.csv"), "Windows-31J");

            assertThat(csvContent.get(5)[0], is("12345683"));
            assertThat(csvContent.get(5)[1], is("追六 太郎2"));
            assertThat(csvContent.get(5)[2], is("オイロク タロウニ"));
            assertThat(csvContent.get(5)[3], is("15"));
            assertThat(csvContent.get(5)[4], is("2012/02/01 00:00:00"));
            assertThat(csvContent.get(5)[5], is("1"));

            assertThat(csvContent.get(6)[0], is("12345684"));
            assertThat(csvContent.get(6)[1], is("追六 太郎3"));
            assertThat(csvContent.get(6)[2], is("オイロク タロウサン"));
            assertThat(csvContent.get(6)[3], is("16"));
            assertThat(csvContent.get(6)[4], is("2012/02/01 00:00:00"));
            assertThat(csvContent.get(6)[5], is("0"));

        } catch (FileNotFoundException e) {
            e.printStackTrace();
            fail();
        } catch (IOException e) {
            e.printStackTrace();
            fail();
        }

        // mailFrom
        PointBalanceService interPointFrm = new PointBalanceService();
        InternetAddress resultMailAddressFrom = new InternetAddress();
        resultMailAddressFrom.setAddress("example@email.com");
        interPointFrm.setResultMailAddressFrom(resultMailAddressFrom);

        assertThat(resultMailAddressFrom.getAddress(), is("example@email.com"));

        assertThat(interPointFrm.getResultMailAddressFrom(),
                is(resultMailAddressFrom));

        // mailTo

        List<InternetAddress> resultMailAddressTo = new ArrayList<InternetAddress>();
        InternetAddress email = new InternetAddress();
        email.setAddress("exampleTo@email.com");
        resultMailAddressTo.add(email);

        assertThat(email.getAddress(), is("exampleTo@email.com"));

    }

    /**
     * Test07 Method Test: execute().
     * 
     * Sub method: createPointBalanceFile().
     * 
     * Test content: output csv file after finishing batch, check path File
     * 
     * Test condition:N/A
     * 
     * Expected Result: create csv file successfully csv file name:pointo.csv
     */

    @SuppressWarnings("unchecked")
    @Test
    public void testKokyakuDaoGetPointBalance07() {

        PointBalanceService pointBalanceService = new PointBalanceService();

        AutoTaskHistory history = new AutoTaskHistory();
        String userId = "9000";
        pointBalanceService.execute(history, userId);

        // Get content of csv file
        try {

            List<String[]> csvContent = CsvUtil.getLines(new File(
                    "file/temp_file/pointo.csv"), "Windows-31J");

            assertThat(csvContent.get(6)[0], is("12345684"));
            assertThat(csvContent.get(6)[1], is("追六 太郎3"));
            assertThat(csvContent.get(6)[2], is("オイロク タロウサン"));
            assertThat(csvContent.get(6)[3], is("16"));
            assertThat(csvContent.get(6)[4], is("2012/02/01 00:00:00"));
            assertThat(csvContent.get(6)[5], is("0"));

        } catch (FileNotFoundException e) {
            e.printStackTrace();
            fail();
        } catch (IOException e) {
            e.printStackTrace();
            fail();
        }

        // mailFrom
        PointBalanceService interPointFrm = new PointBalanceService();
        InternetAddress resultMailAddressFrom = new InternetAddress();
        resultMailAddressFrom.setAddress("example@email.com");
        interPointFrm.setResultMailAddressFrom(resultMailAddressFrom);

        assertThat(resultMailAddressFrom.getAddress(), is("example@email.com"));

        assertThat(interPointFrm.getResultMailAddressFrom(),
                is(resultMailAddressFrom));

        // mailTo

        List<InternetAddress> resultMailAddressTo = new ArrayList<InternetAddress>();
        InternetAddress email = new InternetAddress();
        email.setAddress("exampleTo@email.com");
        resultMailAddressTo.add(email);

        assertThat(email.getAddress(), is("exampleTo@email.com"));

    }

    /**
     * Test003 Method Test: excute().
     * 
     * Sub method: createPointSummaryFile().
     * 
     * Test content: output csv file after finishing batch., check path File
     * 
     * Test condition:N/A
     * 
     * Expected Result: create csv file successfully csv file
     * name:pointo_summary.csv content of csv file:91
     */

    @SuppressWarnings("unchecked")
    @Test
    public void testKokyakuDaoGetPointBalance003() {

        PointBalanceService pointBalanceService = new PointBalanceService();

        AutoTaskHistory history = new AutoTaskHistory();
        String userId = "9000";
        pointBalanceService.execute(history, userId);

        // Get content of csv file
        try {

            List<String[]> csvContent = CsvUtil.getLines(new File(
                    "file/temp_file/pointo_summary.csv"), "UTF-8");

            assertThat(csvContent.get(0)[0], is("91"));

        } catch (FileNotFoundException e) {
            e.printStackTrace();
            fail();
        } catch (IOException e) {
            e.printStackTrace();
            fail();
        }

    }

    /**
     * Test004 Method Test: excute().
     * 
     * Sub method: createPointSummaryFile().
     * 
     * Test content: output csv file after finishing batch.
     * 
     * Test condition:TORIHIKISAKI_ID=19
     * 
     * Expected Result: create csv file unsuccessfully pointo_summary.csv total
     * content of csv = 91
     * 
     */

    @SuppressWarnings("unchecked")
    @Test
    public void testKokyakuDaoGetPointBalance004() {

        PointBalanceService pointBalanceService = new PointBalanceService();

        AutoTaskHistory history = new AutoTaskHistory();
        String userId = "9000";
        pointBalanceService.execute(history, userId);

        // Get TORIHIKISAKI_ID
        EntityManager em = EntityManagerFactory.getEntityManager();
        List<Map<String, Object>> resultMap = new ArrayList<Map<String, Object>>();
        StringBuilder sb = new StringBuilder();
        sb.append("select TORIHIKISAKI_ID from m_Kokyaku");

        SQLQuery query = em.createSQLQuery(sb.toString());
        query.addScalar("TORIHIKISAKI_ID", Hibernate.STRING);

        resultMap = query.setResultTransformer(
                CriteriaSpecification.ALIAS_TO_ENTITY_MAP).list();

        // Confirm Sitecode
        assertThat(resultMap.get(9).size(), is(1));
        // assertThat((String) resultMap.get(9).get("TORIHIKISAKI_ID"),
        // is("19"));

        // Get pointBalanceList

        List<Map<String, Object>> pointBalanceList = KokyakuDao
                .getPointSummaryBalance();

        // Get content of csv file
        try {

            List<String[]> csvContent = CsvUtil.getLines(new File(
                    "file/temp_file/pointo_summary.csv"), "UTF-8");

            assertThat(csvContent.get(0)[0], is("91"));

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    /**
     * Test005 Method Test: excute().
     * 
     * Sub method: createPointSummaryFile().
     * 
     * Test content: output csv file after finishing batch.
     * 
     * Test condition:TORIHIKISAKI_ID=17
     * 
     * Expected Result: create csv file unsuccessfully pointo_summary.csv total
     * content of csv = 91
     * 
     */

    @SuppressWarnings("unchecked")
    @Test
    public void testKokyakuDaoGetPointBalance005() {

        PointBalanceService pointBalanceService = new PointBalanceService();

        AutoTaskHistory history = new AutoTaskHistory();
        String userId = "9000";
        pointBalanceService.execute(history, userId);
        // List

        List<Map<String, Object>> pointBalanceList = KokyakuDao
                .getPointSummaryBalance();

        // Get content of csv file
        try {

            List<String[]> csvContent = CsvUtil.getLines(new File(
                    "file/temp_file/pointo_summary.csv"), "UTF-8");
            assertThat(csvContent.get(0)[0], is("91"));
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        // mailFrom
        PointBalanceService service = new PointBalanceService();
        InternetAddress resultMailAddressFrom = new InternetAddress();
        service.setResultMailAddressFrom(resultMailAddressFrom);
        assertThat(service.getResultMailAddressFrom(),
                is(resultMailAddressFrom));

        // mailTo

        List<InternetAddress> resultMailAddressTo = new ArrayList<InternetAddress>();

        service.setResultMailAddressTo(resultMailAddressTo);
        assertThat(service.getResultMailAddressTo(), is(resultMailAddressTo));

    }

    /**
     * Test006 Method Test: execute().
     * 
     * Sub method: createPointSummaryFile().
     * 
     * Test content: output csv file after finishing batch.
     * 
     * Test condition:Pointo > 0
     * 
     * Expected Result: create csv file of unsuccessfully pointo_summary.csv
     * total content of csv = 91
     * 
     */

    @SuppressWarnings("unchecked")
    @Test
    public void testKokyakuDaoGetPointBalance006() {

        PointBalanceService pointBalanceService = new PointBalanceService();

        AutoTaskHistory history = new AutoTaskHistory();
        String userId = "9000";
        pointBalanceService.execute(history, userId);
        // List

        List<Map<String, Object>> pointBalanceList = KokyakuDao
                .getPointSummaryBalance();

        // Get content of csv file
        try {

            List<String[]> csvContent = CsvUtil.getLines(new File(
                    "file/temp_file/pointo_summary.csv"), "UTF-8");
            assertThat(csvContent.get(0)[0], is("91"));
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    /**
     * Test007 Method Test: Minus between 2 case
     * 
     * Sub method: createPointSummaryFile().
     * 
     * Test content: output csv file after finishing batch.
     * 
     * Test condition: TORIHIKISAKI_ID != 17 , TORIHIKISAKI_ID != 19 , POINTO >
     * 0, GOCHI_FLG = (0:oisix or 1:gochimaru)
     * 
     * Expected Result: create csv file of unsuccessfully pointo_summary.csv
     * 
     * in 1 summarypointo=135 in 2 summarypointo(incasenotmatch)=44
     * 1-2=summarypoint successfull =91
     */

    @SuppressWarnings("unchecked")
    @Test
    public void testKokyakuDaoGetPointBalance007() {

        PointBalanceService pointBalanceService = new PointBalanceService();

        AutoTaskHistory history = new AutoTaskHistory();
        ExecuteDecision execute = new ExecuteDecision() {
            @Override
            public boolean isExecutable(Date date) {
                return true;
            }

            @Override
            public String getTimingCaption() {
                return "abc";
            }
        };
        pointBalanceService.setExecuteDecision(execute);
        String userId = "9000";

        pointBalanceService.execute(history, userId);

        pointBalanceService.isExecutable(new Date());
        assertTrue(pointBalanceService.isExecutable(null));
        assertThat(pointBalanceService.getTimingCaption(), is("abc"));
        assertThat(pointBalanceService.getExecuteDecision(), is(execute));

        // select summary of all excel file

        EntityManager em = EntityManagerFactory.getEntityManager();
        List<Map<String, Object>> resultCaseone = new ArrayList<Map<String, Object>>();
        StringBuilder sb = new StringBuilder();
        sb.append("select SUM(pointo) AS totalPoint from m_Kokyaku");

        SQLQuery query = em.createSQLQuery(sb.toString());
        query.addScalar("totalPoint", Hibernate.STRING);

        resultCaseone = query.setResultTransformer(
                CriteriaSpecification.ALIAS_TO_ENTITY_MAP).list();

        // Confirm
        assertThat((String) resultCaseone.get(0).get("totalPoint"), is("135"));

        // mailFrom
        PointBalanceService service = new PointBalanceService();
        InternetAddress resultMailAddressFrom = new InternetAddress();
        service.setResultMailAddressFrom(resultMailAddressFrom);
        assertThat(service.getResultMailAddressFrom(),
                is(resultMailAddressFrom));

        // mailTo

        List<InternetAddress> resultMailAddressTo = new ArrayList<InternetAddress>();

        service.setResultMailAddressTo(resultMailAddressTo);
        assertThat(service.getResultMailAddressTo(), is(resultMailAddressTo));

    }

    /**
     * Test008 Method: createCsvFolder
     * 
     * Test content: check an error in throws f.mkdirs
     * 
     * Test condition: !f.mkdirs = system not create in directory
     * 
     * Expected Result: Message will show ("Unable to create directory " + f)
     * 
     */

    @SuppressWarnings("unchecked")
    @Test
    public void testKokyakuDaoGetPointBalance008() {

        PointBalanceService service = new PointBalanceService();
        System.setProperty("oisix.file.root", "3554@@@@@");
        StringBuilder strDir = new StringBuilder();

        /*
         * List<Map<String, Object>> resultMap = new ArrayList<Map<String,
         * Object>>(); StringBuilder strDir = new StringBuilder();
         * strDir.append(service.getFileFolderPath());
         */

        try {
            File f = new File(strDir.toString());
            if (!f.exists()) {
                if (!f.mkdirs()) {
                    // ถ้าไม่สามารถสร้างได้
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            assertThat(e.getMessage(),
                    is("Unable to create directory 3554@@@@@"));
            fail();
        }
    }

    /**
     * Test009 Method: createPointBalanceFile
     * 
     * Test content: check an error in throw of 
     * 
     * Test condition:
     * 
     * Expected Result: throws messsage  "Cannot Create Point Balance File"
     * 
     */

    @SuppressWarnings("unchecked")
    @Test
    public void testKokyakuDaoGetPointBalance009() {

        PointBalanceService service = new PointBalanceService();
        CsvWriter csvWriter = new CsvWriter(service.createCsvFolder());

            System.setProperty("site.code", "1");
            PointBalanceService pointBalanceService = new PointBalanceService();

            AutoTaskHistory history = new AutoTaskHistory();

            String userId = "9000";
            TestException.set(pointBalanceService, "CannotCreatePointBalanceFile");
            AutoTaskResult result = pointBalanceService.execute(history, userId);
            assertFalse(result.isSuccess());
    }


    /**
     * Test010 Method: createPointBalanceSummaryFile
     * 
     * Test content: check an error in throw 
     * 
     * Test condition:
     * 
     * Expected Result:Show an error 
     * 
     */

    @SuppressWarnings("unchecked")
    @Test
    public void testKokyakuDaoGetPointBalance010() {

        PointBalanceService service = new PointBalanceService();

            System.setProperty("site.code", "1");
            PointBalanceService pointBalanceService = new PointBalanceService();

            AutoTaskHistory history = new AutoTaskHistory();

            String userId = "9000";
            TestException.set(pointBalanceService, "CannotCreatePointSummaryFile");
            AutoTaskResult result = pointBalanceService.execute(history, userId);
            assertFalse(result.isSuccess());
    }
    
    


    /**
     * Test008 Method Test: Minus between 2 case
     * 
     * Sub method: createPointSummaryFile().
     * 
     * Test content: output csv file after finishing batch.
     * 
     * Test condition: TORIHIKISAKI_ID != 17 , TORIHIKISAKI_ID != 19 , POINTO >
     * 0, GOCHI_FLG = (0:oisix or 1:gochimaru)
     * 
     * Expected Result: create csv file of unsuccessfully pointo_summary.csv
     * 
     * in 1 summarypointo=135 in 2 summarypointo(incasenotmatch)=44
     * 1-2=summarypoint successfull =91
     * 
     * @Test public void testKokyakuDaoGetPointBalance008() {
     * 
     *       PointBalanceService pointBalanceService = new
     *       PointBalanceService();
     * 
     *       AutoTaskHistory history = new AutoTaskHistory();
     * 
     *       String userId = "9000";
     * 
     *       pointBalanceService.execute(history, userId);
     * 
     *       // select summary of wrong case in excel file EntityManager em =
     *       EntityManagerFactory.getEntityManager(); List<Map<String, Object>>
     *       resultCaseTwo = new ArrayList<Map<String, Object>>();
     * 
     *       StringBuilder sb = new StringBuilder(); // calculate summary pointo
     *       of all customer not match sql condition
     * 
     *       sb.append(
     *       "select SUM(pointo) AS totalPoint from m_Kokyaku WHERE torihikisaki_id=17 OR torihikisaki_id=19 OR pointo=0 OR (gochiFlg !=1 AND gochiFlg!=0)"
     *       ); SQLQuery query = em.createSQLQuery(sb.toString());
     *       query.addScalar("totalPoint", Hibernate.STRING);
     * 
     *       // Confirm assertThat((String)
     *       resultCaseTwo.get(0).get("totalPoint"), is("44")); }
     */
    @After
    public void tearDown() throws Exception {
    }

    @AfterClass
    public static void cleanUp() {
        testData.cleanup();
        testData = null;
    }

}
